﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using PictureTool.Model;

namespace PictureTool
{
    public partial class Main2 : Form
    {
        private string imgName = string.Empty;
        private string imgPath = string.Empty;
        private int imgH = 0;
        private int imgW = 0;

        public Main2()
        {
            InitializeComponent();
        }

        private void btnGetImgMsg_Click(object sender, EventArgs e)
        {
            try
            {
                string imgDirPath = string.Format("{0}\\{1}", Config.PicturePath, tBoxPicName.Text.Trim());
                string[] files = System.IO.Directory.GetFiles(imgDirPath);
                // MessageBox.Show(Path.GetFileName(files[0]));
                //tBoxPicName.Text = Path.GetFileName(files[0]);
                imgName = Path.GetFileName(files[0]);
                imgPath = string.Format("{0}\\{1}", imgDirPath, imgName);
                Image img = Image.FromFile(imgPath);
                string imgMsg = string.Format("{0}|图片高度:{1}px,图片宽度:{2}", imgName, img.Height, img.Width);
                labShowImgMsg.Text = imgMsg;
                tBX.Text = "0";
                tBY.Text = "0";
                tBW.Text = img.Width.ToString();
                tBH.Text = img.Height.ToString();

                imgH = img.Height;
                imgW = img.Width;

                tBoutPut.Text = string.Format("{0}\\ok", imgDirPath);
            }
            catch (Exception)
            {
                MessageBox.Show("没有找到图片");
                return;
            }
           
        }

        private void Main_Load(object sender, EventArgs e)
        {
            labSorceImgPath.Text = Config.PicturePath;
            tBoutPut.Text = Config.OutPutPath;
            appContainer1.AppFilename = @"C:\Windows\System32\mspaint.exe";
            appContainer1.Start();
        }

        private void btnCutImg_Click(object sender, EventArgs e)
        {
            List<RectangleModel> list = new List<RectangleModel>();
            for (int i = 0; i < this.dataGridView1.Rows.Count-1;i++ )
            {
                int x = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value);
                int y = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[1].Value);
                int w = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[2].Value);
                int h = Convert.ToInt32(this.dataGridView1.Rows[i].Cells[3].Value);
                list.Add(new RectangleModel(x,y,w,h));
            }          
                CutImgKit(list);
        }

        private void CutImgKit(List<RectangleModel> list)
        {            
            try{
                Bitmap bmpBase = new Bitmap(imgPath);//构造图片对象
                string newFilePath=string.Empty;
                Bitmap bmpNew=null;
                for(int i=0;i<list.Count;i++){
                    Rectangle rect = new Rectangle(list[i].X, list[i].Y, list[i].H, list[i].W);                 
                   bmpNew = bmpBase.Clone(rect, bmpBase.PixelFormat);//将这个区域克隆成新的图片
                   newFilePath = string.Format("{0}\\{1}", tBoutPut.Text, i+".jpg");
                   bmpNew.Save(newFilePath, ImageFormat.Jpeg);//保存
                }

                bmpBase.Dispose();// 释放资源
                bmpNew.Dispose();//释放资源
                 
            }catch(Exception){
                MessageBox.Show("截图失败");
                return;
            }

            MessageBox.Show("截图成功");
        }
      

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int index = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[index].Cells[0].Value = tBX.Text;
            this.dataGridView1.Rows[index].Cells[1].Value = tBY.Text;
            this.dataGridView1.Rows[index].Cells[2].Value = tBH.Text;
            this.dataGridView1.Rows[index].Cells[3].Value = tBW.Text;
            tBY.Text = (Convert.ToInt32(tBY.Text) + Convert.ToInt32(tBH.Text)).ToString();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
            {
                this.dataGridView1.Rows.RemoveAt(i);
            }          
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {   //Y坐标+高度 =变更之后的Y坐标
            this.yAfter.Text = (Convert.ToInt32(tBY.Text) + Convert.ToInt32(tBH.Text)).ToString();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {  //高度 = 变更之后的Y坐标-Y坐标
            tBH.Text = (Convert.ToInt32(yAfter.Text) - Convert.ToInt32(tBY.Text)).ToString();
        }

        private void btnClearFiles_Click(object sender, EventArgs e)
        {
            string imgName0 = string.Empty;
            string imgPath0 = string.Empty;
            try
            {
                string[] files = System.IO.Directory.GetFiles(tBoutPut.Text);
                for (int i = 0; i < files.Length; i++)
                {
                    imgName0 = Path.GetFileName(files[i]);
                    imgPath0 = string.Format("{0}\\{1}", tBoutPut.Text, imgName0);
                    System.IO.FileInfo file = new System.IO.FileInfo(imgPath0);
                    if (file.Exists)//文件是否存在，存在则执行删除  
                    {
                        file.Delete();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("删除失败");
                return;
            }

            MessageBox.Show("删除成功");

        }

       
    }
}
